package BUCF;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.travel.core.AbstractDao;

public class similarityUsers {
    String source;
    int n;

    public similarityUsers() {
    }

    public similarityUsers(String source, int n) {
        this.source = source;
        this.n = n;
    }

    public Set<user_sim> getUsers() {
        List<user_sim> temp_uslist = new ArrayList<user_sim>();
        Set<user_sim> userNames = new HashSet<user_sim>();
        List<user_sim> uslist = new ArrayList<user_sim>();
        // Set<String> users=JdbcUtils.getAllUsers();
        Set<String> users = AbstractDao.getValuableUsers(source);
        // System.out.println(users.size());
        int j = 0;
        for (String user : users) {
            // System.out.println(user);
            if (j < 50) {
                if (!user.equals(source)) {
                    double sim = new similarity(source, user).computeSimilarity();
                    if (sim > 0) {
                        // System.out.println(sim);
                        uslist.add(new user_sim(user, sim));
                    }
                }
                j++;
            } else {
                break;
            }
        }
        Collections.sort(uslist, new mycomparator());
        int num = 0;
        for (user_sim us : uslist) {
            // System.out.println(us.getUser()+" "+us.getSim());
            if (num < n) {
                temp_uslist.add(us);
                num++;
            } else {
                break;
            }
        }
        for (user_sim us : temp_uslist) {
            String userId = us.getUser();
            // System.out.println(userId);
            String userName = AbstractDao.getUserName(userId);
            double sim = us.getSim();
            // System.out.println(userName);
            userNames.add(new user_sim(userName, sim));
        }
        // Collections.sort(userNames, new mycomparator());
        return userNames;
    }

    public static void main(String[] args) {
        Set<user_sim> users = new similarityUsers("17608579", 10).getUsers();
        for (user_sim user : users) {
            System.out.println(user.getUser() + ":" + user.getSim());
        }
    }
}
